package com.weixx;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;

import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

public class WeixxCode {

    public static void main(String[] args) {
        //需要构造一个代码生成器 对象
        AutoGenerator mpg = new AutoGenerator();
        //配置策略
        //1.全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setAuthor("weixx");
        gc.setOpen(false);
        gc.setFileOverride(false);//文件是否覆盖
        gc.setServiceName("%sService");//去掉Service的i前缀
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);
        //2.设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://47.101.36.174:3308/mybaits-plus?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
        //3.包的配置
        PackageConfig pc  = new PackageConfig();
        pc.setModuleName("autogen");
        pc.setParent("com.weixx");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setController("controller");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        //4.策略配置
        StrategyConfig sc = new StrategyConfig();
        sc.setInclude("user");//设置要映射的表名字,逗号分割多个表
        sc.setNaming(NamingStrategy.underline_to_camel);//命名规则，下划线转驼峰
        sc.setColumnNaming(NamingStrategy.underline_to_camel);//命名规则，列，下划线转驼峰
        //sc.setSuperEntityClass("自己的父类实体，没有就不用设置！");
        sc.setEntityLombokModel(true);//自动lombok
        sc.setLogicDeleteFieldName("delete");//逻辑删除字段
        //自动填充配置
        TableFill gmt_create = new TableFill("gmt_create",FieldFill.INSERT);
        TableFill gmt_modified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFillArrayList=new ArrayList<>();
        tableFillArrayList.add(gmt_create);
        tableFillArrayList.add(gmt_modified);
        sc.setTableFillList(tableFillArrayList);

        sc.setRestControllerStyle(true);//restFul驼峰命名规则
        sc.setControllerMappingHyphenStyle(true);//localhost:8080/hello_id_2
        //乐观锁
        sc.setVersionFieldName("version");


        mpg.setStrategy(sc);

        mpg.execute();



    }
}
